# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=spirv-cross
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
epoch=1
pkgver=1.3.224.1
pkgrel=1
pkgdesc="A tool and library for parsing and converting SPIR-V to other shader languages (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/KhronosGroup/SPIRV-Cross/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-glslang"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-nose"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers"
             "${MINGW_PACKAGE_PREFIX}-spirv-tools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://github.com/KhronosGroup/SPIRV-Cross/archive/refs/tags/sdk-${pkgver}.tar.gz)
sha512sums=('2747b1005845c0e1c15360022e7719d0f778b8e6ba2e62214532cd821e7fda82c899c3c5336c398ba45b1105c7accff6c1d00792054a4ec782d1618a945b7dcf')

prepare() {
  cd "${srcdir}/SPIRV-Cross-sdk-${pkgver}"

  # Use system provided glslang and spirv-tools binaries.
  sed -e "s|NO_DEFAULT_PATH||g" -i CMakeLists.txt
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DSPIRV_CROSS_SHARED=ON \
    -DSPIRV_CROSS_ENABLE_TESTS=ON \
    "../SPIRV-Cross-sdk-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -Dm644 "${srcdir}/SPIRV-Cross-sdk-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
